package com.kyh.service.impl;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.kyh.entity.Item;
import com.kyh.mapper.ItemMapper;
import com.kyh.service.ItemService;
import org.springframework.transaction.annotation.Transactional;
import com.kyh.vo.BootstrapTableVoJA;
import com.kyh.vo.R;

@Service("itemService")
public class ItemServiceImpl implements ItemService {

	@Autowired
	private ItemMapper itemMapper;

	@Override
	@Transactional
	public R add(Item entity) throws Exception {
		
		// 添加数据
		if (itemMapper.insert(entity) != 1) {
			throw new RuntimeException("失败");
		}
		
		return R.ok();
	}

	@Override
	@Transactional
	public R deleteFalse(Long id) throws Exception {
	
		if (itemMapper.deleteFalse(id) != 1) {
			throw new RuntimeException("失败");
		}
		
		return R.ok();
	}

	@Override
	@Transactional
	public R deleteTrue(Long id) {
		
		Item entity = itemMapper.get(id);
		if (entity == null) {
			return R.error("该数据不存在");
		}
		
		itemMapper.deleteTrue(id);
		
		return R.ok();
	}

	@Override
	@Transactional
	public R edit(Item entity) throws Exception {
		
		// 更新数据
		if (itemMapper.update(entity) != 1) {
			throw new RuntimeException("失败");
		}
		
		return R.ok();
	}

	@Override
	public BootstrapTableVoJA search(Item cond) {
		
		List<Map<String, Object>> listMap = cond.queryMap();
		
		int total = cond.count();
		
        BootstrapTableVoJA vo = new BootstrapTableVoJA();
        vo.setRowsByListMap(listMap);
        vo.setTotal(total);
        
        return vo;
	}
}
